package com.smz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smz.dao.TPrinterDAO;
import com.smz.dao.TProductOrderDAO;
import com.smz.entity.TPrinter;
import com.smz.entity.TProductOrder;
import com.smz.service.TPrinterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Kan
 * @Time 2021-11-21-18:36
 */
@Service
public class TPrinterServiceImpl extends ServiceImpl<TPrinterDAO, TPrinter> implements TPrinterService {
    @Autowired
    TPrinterDAO tPrinterDAO;


    @Autowired
    TProductOrderDAO tProductOrderDAO;
    @Override
    public List<TPrinter> findAllPrinters() {
        return tPrinterDAO.findAllPrinters();
    }

    @Override
    public List<TPrinter> findPrinters(String pNum) {
        return tPrinterDAO.findPrinter(pNum);
    }

    @Transactional
    @Override
    public String addPrinter(TPrinter tPrinter) {
        //判断
        QueryWrapper<TPrinter> wrapper = new QueryWrapper<>();
        wrapper.eq("p_num",tPrinter.getPNum());
        TPrinter one = tPrinterDAO.selectOne(wrapper);
        if (one==null){
            tPrinterDAO.insert(tPrinter);
            return "ok";
        }else {
            return "no";
        }
    }

    @Transactional
    @Override
    public String testPrintNum(String pNum) {
        QueryWrapper<TPrinter> wrapper = new QueryWrapper<>();
        wrapper.eq("p_num",pNum);
        wrapper.select("p_num");
        TPrinter tPrinter = tPrinterDAO.selectOne(wrapper);
        if (tPrinter==null){
            return "ok";
        }else {
            return "no";
        }
    }

    @Transactional
    @Override
    public String updatePrinter(TPrinter tPrinter) {
        QueryWrapper<TPrinter> wrapper = new QueryWrapper<>();
        wrapper.eq("p_num",tPrinter.getPNum());
        TPrinter one = tPrinterDAO.selectOne(wrapper);
        if (one!=null){
            tPrinterDAO.updateById(tPrinter);
            return "ok";
        }else {
            return "no";
        }
    }

    @Transactional
    @Override
    public String removePrinter(TPrinter tPrinter) {
        QueryWrapper<TProductOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("printer",tPrinter.getPNum());
        wrapper.select("printer");
        TProductOrder order = tProductOrderDAO.selectOne(wrapper);
        if (order==null){
            tPrinterDAO.deleteById(tPrinter);
            return "ok";
        }else {
            return "no";
        }
    }

    @Transactional
    @Override
    public String removePrinterBranch(String[] pNums) {
        Map<String, Object> queryMap = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        for (String pNum : pNums) {
            queryMap.put("printer",pNum);
            if (tProductOrderDAO.selectByMap(queryMap)==null){
                map.put("p_num",pNum);
                tPrinterDAO.deleteByMap(map);
                map.clear();
            }else {
                stringBuilder.append(pNum+",");
            }
            queryMap.clear();
        }
        return stringBuilder.toString();
    }


}
